home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Presentations / Presentations ’96 / Papers ’96 / Standard Template Library / STL Library for Macintosh.text < prev    next >
Encoding:
Text File  |  1996-06-21  |  24.4 KB  |  526 lines  |  [TEXT/MSWD]

  1.  
  2. The Standard Template Library and Macintosh Programming
  3.  
  4. George G. Geller, Ph.D.
  5. Macintosh programmer and consultant.
  6. email: 71321,2544@compuserve.com
  7.  
  8. The Standard Template Library (STL) was developed by Alexander Stepanov and Meng Lee.  This large and innovative body of code has been adopted as an important part of the new ANSI draft standard C++ and promises to dramatically influence the way C++ programmers work.
  9.  
  10. STL is simultaneously efficient, general, and compact in representation.  Its central paradigm is the decomposition of programming tasks into generalized algorithms that work on 'containers'.  The containers hold native C++ data types or user-defined objects.  This approach, known as generic programming, drastically reduces the code base necessary to handle many common programming tasks.
  11.  
  12. In this presentation I will introduce STL and show examples of its application in Macintosh programming.  The current state of STL support in Macintosh C++ compilers will also be reviewed.
  13.  
  14.  
  15. In this paper I will demonstrate how to use the Standard Template Library (STL) to help you with your Macintosh C++ programming.  Although Alexander Stepanov, the principal author of the library, has a mathematical background which is readily apparent in the published specifications I won't spend a lot of time showing you a bunch of abstractions.  I'll just go over the basics that you absolutely need to understand so that you can follow the concrete examples that I will present later.
  16.  
  17. In order to use STL you must have a good grasp of C++ templates.  You have to understand some details about how your development environment implements templates.  And, more likely than not, you have to get used to looking at template code in your debugger.
  18.  
  19. In exchange for this small investment, STL gives you an immense payback.  To start with, you no longer have to implement most types of data structures; vectors (which are equivalent to dynamic arrays), linked lists, b-trees, and associative arrays are all provided.  You no longer have to implement many common algorithms such as sorting and searching.  Also, STL handles many issues of memory management automatically for you -- you almost never need to use the C++ new and delete.
  20.  
  21. STL will be part of the next ANSI standard.  That means it will be included with any C++ compiler on any platform.  Since it is a standard, you can be confident that code you write using STL will work in the future and with any hardware platform or compiler.  
  22.  
  23. To get started with STL, you should probably read one of the introductory books I list in the bibliography.  I recommend Mark Nelson's "C++ Programmer's Guide to the Standard Template Library."
  24.  
  25. Comments about efficiency:  In general the performance matches what you get for hand-coded C.  The advanced container classes (set, multiset, map, and mulitmap) in STL use red-black trees.  Alexander Stepanov, the principal author of STL, believes that this is the best structure for something that will reside in memory.  Anyone is free to try their hand at substituting another structure such as a B*tree, skip lists, splay trees, or half-balanced trees.  Comparing the performance of the various structures could become a major research project.
  26.  
  27. Much of my paid work involves porting applications from other platforms (whose names needn't be mentioned here, since we don't have barf bags handy) to the Macintosh.  A really cute trick that the implementors of STL pulled off was to isolate the machine dependencies of the memory model to their allocator class.  This leaves the code you actually work with looking very clean.
  28.  
  29. What is STL?  It is fundamentally a collection of container templates, which are classes, and algorithm or function templates.  Supporting these is the concept of an iterator.  An iterator is a generalized pointer.  Just as you de-reference a pointer in C to gain access to a member of an array, you use an iterator to gain access to an object stored in one of STL's container classes.  There are several types of iterators.  They all support the basic operations of incrementing and dereferencing.  If you use the right kind of iterator object (an insertion iterator), STL's container classes automatically grow to accommodate however many elements you want to add, up to the limit of available RAM.
  30.  
  31. It is vital to understand that STL algorithms are not encapsulated in classes.  They are function templates that operate on the container classes.  This means that most algorithms work with most container classes.  However, it does make "naked" STL harder to use than a typical class library.  One solution to this is to write specialized wrapper classes that use STL.  In the example program for this talk I used this approach for my macString class which transparently handles the chore of dealing with Pascal and C style strings in Macintosh programming.  MacString objects uses the STL vector container class to store the string data.  There are commercial libraries available, from RogueWave and probably others, that use the class wrapper approach to hide the complexity of naked STL.
  32.  
  33. Perhaps the strongest motivation for adopting STL and the generic programming approach is that it vastly reduces the amount of coding you have to do.  You get this reduction because you (or a library author) write a container class only once, then use C++ templates and generic programming to apply various algorithms to your container class.  The algorithm, say a sort, is written only once and is applicable to different types of containers such as arrays or queues.  This is the central paradigm that I mentioned in the abstract.  Consider this example: you have three different classes of objects, lets call them strings, window pointers, and sprites.  And you have three different types of containers that you want to use to store these objects in, vectors, queues, and stacks.  And you need insertion, extraction, and sorting algorithms for every combination of object and container.  That leaves you with a total of 27 (3x3x3) algorithms to implement.  If you use generic programming techniques you reduce the number of algorithms to just 3.  This is a very simple example.  In real world-applications, the benefits are potentially much greater.
  34.  
  35. STL is open.  The source code is provided and is an a form that is easy to use and modify.  You can write a new algorithm and apply it to any of STL's container classes.  Conversely, you can write a new container class and use STL's library of algorithms on it.
  36.  
  37. What is missing from STL?  Most notably, persistence.  There is nothing built-in that lets you store and retrieve your objects and data structures to a file.  There are commercial extensions to STL, from vendors such as RogueWave and ObjectSpace, that include persistent object databases.  There are also hints (in the interview of Alexander Stepanov for the March 1995 Dr. Dobbs Journal) that future versions of STL will include persistent objects.  Also there is nothing analogous to a class browser.
  38.  
  39. What will you find in STL?  You'll find a great implementation of containers and algorithms.  Alexander Stepanov has spent two decades working on generic programming and the ideas behind STL.  The classes and functions are in there.  There is a heck of a lot of functionality.  It is up to you, the programmer, to know where it is and how to use it.  You'll need to keep a reference book handy.
  40.  
  41. STL example program for the Macintosh
  42.  
  43. I wrote a program called Dupes to demonstrate using STL programming techniques on the Macintosh.  Dupes catalogs your hard disk and creates a listing of files that have duplicate names.
  44.  
  45. From the pedagogic standpoint there are two primary points to be made about Dupes.
  46.  
  47. First, I created a macString class that uses the STL vector container to store string data in a form that is easy to access and manipulate.  This class has member functions that allow transparent access to your string object as a C string or a Pascal string.  The following code snippet illustrates:
  48.     #include "macString.h"
  49.     macString ms;
  50.     strcpy(ms, "STL saves work."); /* copy a C string into ms */
  51.     ParamText(ms, 0L, 0L, 0L);     /* access ms as a Pascal string */
  52.     Alert(129, nil);
  53. If you are so inclined, you can reuse the code for the macString class in your own application.
  54.  
  55. Second, Dupes uses the STL multimap container class to store and efficiently access each file name and the full path name in RAM.  The file name (e.g. notes.txt) is used as the key, and the full path name (e.g.hd:MacHack:notes.txt) is stored as a value.  You will see how little coding I had to do to accomplish this.  STL does all the hard work.
  56.  
  57. Let's examine the code for macString.cp (listing 3, below).  The first thing to notice is that we include stl.h.  This, not an object library, is where the Standard Template Library is added to the project.  Note again that all the code for the library is actually in stl.h and some associated text files.  STL is completely open; nothing is held back or hidden.
  58.  
  59. The next interesting thing in macString.cp is a collection of no less than twelve template statements.  These commands instantiate the templates in stl.h to create the type of objects and functions that you need for your application.  Some of the template commands are remarkably complicated.  Fortunately, you don't have to figure them out yourself.  Some development environments implicitly generate the necessary template commands.  In Symantec C++ you can figure out which ones you need based on the linker errors that are generated.  You don't even need to type in the code, just copy it from the error output window.
  60.  
  61. Most of the magic of the macString class is encapsulated in the synchronize function.  Synchronize uses two Boolean data members, bPStrOK and bCStrOK to ensure that the C string and Pascal string contain the same data.  The other member function of macString are responsible for calling synchronize at the appropriate times. 
  62.  
  63. Listing 1 contains the code for Dupes main itself.  In terms of understanding STL, the primary thing to notice is that we use the multimap container class to store the full path name for each file on the disk indexed by the file name.  This is done in the global simply defined by:
  64. mmap m;
  65.  
  66. In main we call CatalogADirectory to fill up the multimap, then we use a while loop to print out the files sorted by name.  Note the conditional for the while loop:
  67. while (i != m.end())
  68. Why can't we use:
  69. while (i < m.end()) /* !!wrong!! */
  70. The answer is that as an iterator into a multimap, the acutal numerical value of i has no meaning.  The valid operations for this type of iterator are dereferencing, copying, and incrimenting.
  71.  
  72. In main's next while loop, we erase all the entries in the map that correspond to files with unique names.   The name is unique if the key is unique.  The count function of STL multimap return the number of entries that have the supplied key.  We want to erase those entries using the erase function (i.e. cout() == 1).  But wait!  After we erase an entry with m.erase(i), iterator i is no longer valid.  So, we store the value of i in a tempory variable j that we can use the next time through the loop.
  73.  
  74. The last thing to do in main is to type out the list of duplicate file names we wanted in the first place.  The final while loop does just this.
  75.  
  76. The other three functions in DupesMain.cp basically deal with the ugliness of the Macintosh file manager.  This is the kind of code I like to write with one eye on THINK Reference and the other eye on my debugger.  CatalogADirectory is a recursive function that starts at an indicated folder and traverses all the contained folders.  In doing so it fills up the multimap global with the names of all the files it finds.
  77.  
  78. NextFileInDirectory is a tiny helper function for CatalogADirectory.  It handles the logic of moving through a folder and makes the actual call to the PBGetCatInfo Macintosh toolbox function.  For more information on PBGetCatInfo and how to use it consult Inside Macintosh or THINK Reference.
  79.  
  80. DirID2FullPath converts a Macintosh directory ID to the corresponding full path name.  Note that it uses macString's += operator to conveniently concatenate the folder names to produce the full path. 
  81.  
  82. I hope you can see how much STL increase your productivity.  Without STL's multimap class, writing Dupes would have been a daunting task.  With STL, it took me about half a day to get the main functionality fo Dupes up and running.  And, as a bonus we get the macString class which should be useful in a variety of contexts.
  83.  
  84. Appendix: Annotated STL Bibliography
  85.  
  86. Graham Glass and Brett Schuchert, "The STL <PRIMER>", Prentice Hall PTR, 1996.  At 327 pages, this is the most concise of the STL books I've seen.  In spite of the word primer in the title, it assumes more sophistication on the part of the reader than the other texts.  It also pushes the ObjectSpace commercial library, an extension of the ANSI STL.  The source for demo programs is on the Symantec Developer's Advantage CD-ROM and is available on the World Wide Web.
  87.  
  88. David R. Musser and Atul Saini, "STL Tutorial and Reference Guide", Addison Wesley, 1996.  This book includes excellent example programs that use STL and generic programming techniques.  The tutorial section is very good.  The reference section is very formal.
  89.  
  90. Mark Nelson, "C++ Programmer's Guide to the Standard Template Library", IDG Books, 1995.  For my taste this is the best single book on STL.  The tutorial introduction starts slowly and covers everything thoroughly.  Each entry in the reference section includes an illustrative source code sample.  An MS-DOS formatted floppy with STL and the source for the example programs is bundled with the book.
  91.  
  92. Alexander Stepanov, "The Standard Template Library", BYTE Magazine, October 1995, Volume 20, Number 10, October 1995.  Brief introduction and historical perspective straight from STL's primary author.
  93.  
  94. Dan Zigmond, "Generic Programming and the C++ STL", Dr. Dobbs's Journal, issue #233, August 1995.  Includes good illustrative source code.
  95.  
  96. Al Stevens, "Alexander Stepanov and STL", Dr. Dobbs's Journal, March 1995.  An interview with STL's primary author.  Contains many historical insights and some speculation about possible future additions to the library.
  97.  
  98. Al Stevens, "The Standard Template Library", Dr. Dobbs's Journal, April 1995.  A very brief introduction with useful source.
  99.  
  100. butler.hpl.hp.com -- The mother lode for the latest STL.
  101.  
  102. /*============================================================================*/
  103.  
  104. Building Dupes
  105.  
  106. Dupes is built from a Symantec C++, version 8 (from the SDA release 5 CD-ROM) project for PowerPC only.  The project has four source files:  DupesMain.cp, macString.cp and DemoSTR.r.  There are also seven runtime libraries: InterfaceLib, MathLib, PPCANSI.o, PPCCPlusLib.o, PPCIOStreams.o, PPCRuntime.o and StcCLib.o.  The output is to a console Window and is echoed to a log file.
  107.  
  108.  
  109.  
  110. /*============================================================================*/
  111.  
  112. // Listing 1: DupesMain.cp
  113.  
  114. #define OLDROUTINELOCATIONS 0
  115. #include <ConditionalMacros.h>
  116. #include <Types.h> // For Str255
  117. #include <Dialogs.h> // For Alert
  118. #include <TextUtils.h> // For c2pstr
  119. #include <PLStringFuncs.h> // For PLstrcpy
  120. #include <ToolUtils.h> // For BitTst
  121. #include <Files.h>
  122.  
  123. #include <console.h>
  124.  
  125. #include <assert.h>
  126. #include <stdlib.h>
  127. #include <iostream.h>
  128.  
  129. #include "macString.h"
  130.  
  131. #define rUserAlert 129
  132.  
  133. typedef multimap<macString,macString, less<macString> > mmap;
  134. mmap m;
  135.  
  136. static void CatalogADirectory(long);
  137. static void DirID2FullPath(long, macString&);
  138. static Boolean NextFileInDirectory(long, HFileInfo&, macString&);
  139.  
  140. long    countFiles = 0;
  141. long    countDirectories = 0;
  142.  
  143. int main()
  144. {
  145.     console_options.ncols = 135;  // Works for 17" monitor.
  146.     console_options.nrows = 50;  // Works for 17" monitor.
  147.     cecho2file("Dupes.log", 0, stdout);
  148.  
  149.     cout << "Dupes: Examining the default volume..." << endl;
  150.     cout << endl;
  151.  
  152.     CatalogADirectory(fsRtDirID);    // start at the root
  153.     cout << endl;
  154.  
  155.     cout << "Total Directories = " << countDirectories << endl;
  156.     cout << "Total Files       = " << countFiles << endl;
  157.     cout << endl;
  158.  
  159.     // At this point all the files are in mmap, indexed by the file name
  160.     // Print out in sorted order
  161.     mmap::iterator i;
  162.     i = m.begin();
  163.     while (i != m.end ())
  164.     {
  165.         cout << (*i).first << " -> " << (*i).second << endl;
  166.         i++;
  167.     }
  168.     cout << endl;
  169.  
  170.     // Remove all the entries that have unique keys
  171.     mmap::iterator j;
  172.     i = m.begin();
  173.     while (i != m.end ())
  174.     {
  175.         const macString& key = (*i).first;
  176.         if (m.count(key) == 1)
  177.         {
  178.             j = i;
  179.             j++;
  180.             m.erase(i);
  181.         }
  182.         else
  183.         {
  184.             for (int ii = m.count(key); ii > 0; ii--)
  185.             {
  186.                 i++; j++;
  187.             }
  188.         }
  189.         i = j;
  190.     }
  191.  
  192.     // print out the list of files with duplicate names
  193.     i = m.begin();
  194.     while (i != m.end ())
  195.     {
  196.         cout << (*i).first << " -> " << (*i).second << endl;
  197.         i++;
  198.     }
  199.     cout << endl;
  200.  
  201.     return EXIT_SUCCESS;
  202. }
  203.  
  204. static void CatalogADirectory(
  205.     long iDir)
  206. {
  207.     HFileInfo    fileInfo;
  208.     macString   sName;
  209.     macString    sDir;
  210.     macString   sFull;
  211.  
  212.     DirID2FullPath(iDir, sDir);
  213.     cout << sDir << endl;
  214.     
  215.     fileInfo.ioFDirIndex = 0;
  216.     fileInfo.ioVRefNum = 0;
  217.     while (NextFileInDirectory(iDir, fileInfo, sName)){
  218.         if (fileInfo.ioFlAttrib & ioDirMask)
  219.         {    // another directory, recurse
  220.             countDirectories++;
  221.             CatalogADirectory(fileInfo.ioDirID);
  222.         }
  223.         else
  224.         {    // file, add to catalog
  225.             countFiles++;
  226.             sFull = sDir + sName;
  227.             cout << sFull << endl;
  228.             m.insert (pair<const macString, macString> (sName, sFull));
  229.         }
  230.     }
  231. }
  232.  
  233. static void DirID2FullPath(
  234.     long dirID,
  235.     macString& sFullPath)
  236. {
  237.     DirInfo    dirInfo;
  238.     Str255 str255Dir;
  239.     OSErr err;
  240.     macString sTemp;
  241.     
  242.     sFullPath = "";
  243.     dirInfo.ioDrParID = dirID;
  244.     dirInfo.ioNamePtr = str255Dir;
  245.     do {
  246.         dirInfo.ioVRefNum = 0;
  247.         dirInfo.ioFDirIndex = -1;
  248.         dirInfo.ioDrDirID = dirInfo.ioDrParID;
  249.         err = PBGetCatInfo((CInfoPBRec *)&dirInfo, false);
  250.         sTemp = sFullPath;
  251.         sFullPath = str255Dir;
  252.         sFullPath += ":";
  253.         sFullPath += sTemp;
  254.     } while (dirInfo.ioDrDirID != fsRtDirID);  // The root, e.g. "pb:"
  255. }
  256.  
  257. static Boolean NextFileInDirectory(
  258.     long iDir,
  259.     HFileInfo& fileInfo,
  260.     macString& s)
  261. {
  262.     fileInfo.ioFDirIndex++;
  263.     fileInfo.ioDirID = iDir;
  264.     fileInfo.ioNamePtr = (unsigned char *)s;
  265.     return  PBGetCatInfo((CInfoPBRec *)&fileInfo, false) == noErr;
  266. }
  267.  
  268. /*============================================================================*/
  269.  
  270. // Listing 2: macString.h
  271.  
  272. #include <stl.h>
  273.  
  274. // The macString class stores a string and is accesible either as a Pascal 
  275. //    string or as a c-string. The data is stored in str, a vector<char> and also
  276. //    in parallel in a Pascal string.
  277. // Bugs:
  278. //    Should issue warnings when a macString with over 255 characters is accessed
  279. //    as a unsigned char * as this could result is truncation and data loss.
  280. class macString{
  281. public:
  282.     macString() : bPStrOK(true), bCStrOK(true), s(256) {}
  283.     macString(const macString&);
  284.     macString(char c) : bPStrOK(false), bCStrOK(true), s(256) {s[0] = c; 
  285.         synchronize();}
  286.     macString(const char *s);
  287.     macString(const unsigned char *s);
  288.  
  289.     operator char *() {return str();}
  290.         operator const char *() {return conststr();}
  291.         operator unsigned char *() {return pstr();}
  292.     operator const unsigned char *() {return constpstr();}
  293.     friend ostream& operator<< (ostream& os, macString& ms)
  294.         {os << ms.conststr(); return os;} 
  295.     friend ostream& operator<< (ostream& os, const macString& ms) 
  296.         {os << ms.conststr(); return os;}
  297.     friend operator< (macString& ms1, macString& ms2) 
  298.         {return strcmp(ms1.conststr(), ms2.conststr()) < 0;}
  299.     friend operator< (const macString& ms1, const macString& ms2) 
  300.         {return strcmp(ms1.conststr(), ms2.conststr()) < 0;}
  301.     friend operator== (macString& ms1, macString& ms2) 
  302.         {return !strcmp(ms1.conststr(), ms2.conststr());}
  303.     friend operator== (const macString& ms1, const macString& ms2) 
  304.         {return !strcmp(ms1.conststr(), ms2.conststr());}
  305.     macString& operator+=(const macString &);
  306.     friend macString operator+ (macString& ms0, macString& ms1);
  307.     macString& operator= (const macString&);
  308.     void dump(void);
  309. private:
  310.     bool bCStrOK;
  311.     bool bPStrOK;
  312.     vector<char> s;
  313.     Str255 str255;
  314.       const char* conststr() const;
  315.     char* str();
  316.     const unsigned char* constpstr();
  317.     unsigned char* pstr() {synchronize(); bCStrOK = false; return str255;}
  318.     void synchronize();
  319. };
  320.  
  321. /*============================================================================*/
  322.  
  323. // Listing 3: macString.cp
  324.  
  325. #define OLDROUTINELOCATIONS 0
  326. #include <ConditionalMacros.h>
  327. #include <TextUtils.h> // For c2pstr
  328. #include <PLStringFuncs.h> // For PLstrcpy
  329.  
  330. #include <assert.h>
  331. #include <iostream.h>
  332. #include <stl.h>
  333.  
  334. // This is the Symantec C++ 8.1 way to instantiate templates.  Don't forget the
  335. //    ';' on the end of the line
  336. template class vector<char>;
  337. template void uninitialized_fill_n(char *,unsigned int,const char&);
  338. template char *uninitialized_copy(char *,char *,char *);
  339. template char *copy_backward(char *,char *,char *);
  340. template char *copy(const char *,const char *,char *);
  341. template char *uninitialized_copy(const char *,const char *,char *);
  342. template void fill_all(char *,char *,const char&);
  343. template void __distance(rb_tree<macString,pair<const macString,macString>,
  344.     select1st<pair<const macString,macString>,macString>,
  345.     less<macString>>::const_iterator,rb_tree<macString,
  346.     pair<const macString,macString>,
  347.     select1st<pair<const macString,macString>,macString>,
  348.     less<macString>>::const_iterator,unsigned int&,bidirectional_iterator_tag);
  349. template void __distance(rb_tree<macString,pair<const macString,macString>,
  350.     select1st<pair<const macString,macString>,macString>,
  351.     less<macString>>::iterator,
  352.     rb_tree<macString,pair<const macString,macString>,
  353.     select1st<pair<const macString,macString>,macString>,
  354.     less<macString>>::iterator,unsigned int&,bidirectional_iterator_tag);
  355. template char *copy(char *,char *,char *);
  356. template insert_iterator<vector<char>> copy(const char *,const char *,
  357.     insert_iterator<vector<char>>);
  358. template insert_iterator<vector<char>> copy(char *,char *,
  359.     insert_iterator<vector<char>>);
  360.  
  361. #include "macString.h"
  362.  
  363. #define VERBOSE 0
  364.  
  365. macString::macString(
  366.     const macString& ms)
  367.     : bCStrOK(true), bPStrOK(false), s(256)
  368. {
  369.     macString &msIn = (macString)ms;
  370.     msIn.synchronize();
  371.     s = ms.s;
  372.     synchronize();
  373. }
  374.  
  375. macString::macString(
  376.     const char *sIn)
  377.     : bCStrOK(true), bPStrOK(false), s(256)
  378. {
  379.     s.reserve(strlen(sIn) + 1);
  380.     strcpy(s.begin(), sIn);
  381.     synchronize();
  382. }
  383.  
  384. macString::macString(
  385.     const unsigned char *sIn)
  386.     : bCStrOK(false), bPStrOK(true), s(256)
  387. {
  388.     PLstrcpy(str255, sIn);
  389.     synchronize();
  390. }
  391.  
  392. const char* macString::conststr() const
  393. {
  394.     macString& ms = (macString)*this;
  395.     ms.synchronize();
  396.     return s.begin();
  397. }
  398.  
  399. const unsigned char* macString::constpstr()
  400. {
  401.     macString& ms = (macString)*this;
  402.     ms.synchronize();
  403.     return str255;
  404. }
  405.  
  406. void macString::synchronize()
  407. {
  408.     assert(bCStrOK || bPStrOK);
  409.     assert(s.capacity() >= 256);
  410.     if (!bCStrOK)
  411.     {
  412.         PLstrcpy((unsigned char *)s.begin(), str255);
  413.         p2cstr((unsigned char *)s.begin());
  414.         bCStrOK = true;
  415.     }
  416.     if (!bPStrOK)
  417.     {
  418.         strncpy((char *)str255, s.begin(), 255);
  419.         c2pstr((char *)str255);
  420.         bPStrOK = true;
  421.     }
  422. }
  423.  
  424. void macString::dump(void)
  425. {
  426.     assert(bCStrOK || bPStrOK);
  427.     assert(s.capacity() >= 256);
  428.     cout << "in macString::dump" << endl;
  429.     cout << "                this = " << this << endl;
  430.     cout << "        s.capacity() = " << s.capacity() << endl;
  431.     cout << "   strlen(s.begin()) = " << strlen(s.begin()) << endl;
  432.     cout << "-->" << *this << "<--" << endl;
  433. }
  434.  
  435. macString& macString::operator+=(const macString &msc)
  436. {
  437.     size_t len;
  438.     
  439.     synchronize();
  440.     macString& ms = (macString)msc;
  441.     ms.synchronize();
  442.     len = strlen(s.begin()) + strlen(ms.s.begin()) + 1;
  443.     s.reserve(len);
  444.     strcat(s.begin(), ms.s.begin());
  445.     bPStrOK = false;
  446.     synchronize();
  447.     return *this;
  448. }
  449.  
  450. macString operator+ (
  451.     macString& ms0,
  452.     macString& ms1)
  453. {
  454.     ms0.synchronize();
  455.     ms1.synchronize();
  456.     macString ms(ms0);
  457.     ms += ms1;
  458.     return ms;
  459. }
  460.  
  461. char* macString::str()
  462. {
  463.     synchronize();
  464.     bCStrOK = false;
  465.     return s.begin();
  466. }
  467.  
  468. macString& macString::operator=(const macString &mscIn)
  469. {
  470.     macString& msIn = (macString)mscIn;
  471.     msIn.synchronize();
  472.     s = msIn.s;
  473.     bCStrOK = true;
  474.     bPStrOK = false;
  475.     synchronize();
  476.     return *this;
  477. }
  478.  
  479. /*============================================================================*/
  480.  
  481. // Listing 4: DemoSTL.r
  482.  
  483. #define SystemSevenOrLater 1
  484. #include "systypes.r"
  485. #include "types.r"
  486.  
  487. #define rUserAlert 129
  488.  
  489. /* this ALRT and DITL are used as an error screen */
  490. resource 'ALRT' (rUserAlert, purgeable) {
  491.     {40, 20, 150, 260},
  492.     rUserAlert,
  493.     {
  494.         OK, visible, silent,
  495.         OK, visible, silent,
  496.         OK, visible, silent,
  497.         OK, visible, silent
  498.     },
  499.     alertPositionMainScreen
  500. };
  501.  
  502.  
  503. resource 'DITL' (rUserAlert, purgeable) {
  504.     {
  505.         {80, 150, 100, 230},
  506.         Button {
  507.             enabled,
  508.             "OK"
  509.         },
  510.         {10, 60, 60, 230},
  511.         StaticText {
  512.             disabled,
  513.             "^0"
  514.         },
  515.         {8, 8, 40, 40},
  516.         Icon {
  517.             disabled,
  518.             2
  519.         }
  520.     }
  521. };
  522.  
  523. The Standard Template Library and Macintosh Programming, Page 12
  524.  
  525.  
  526.